/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.response;

import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.SolidalApp;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockDialogFragment;

public class AcceptDialogFragment extends SherlockDialogFragment {

	private Request request;
	private Bundle requestBundle;

	public AcceptDialogFragment() {
	}

	public AcceptDialogFragment setRequest(Request request) {
		this.request = request;
		return this;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		LayoutInflater inflater = getActivity().getLayoutInflater();

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		final View view = inflater.inflate(R.layout.dialog_accept, null);

		int title = R.string.request_dialog_title_ok;
		
		try {
		
			// update request
			if (request!=null) {
				requestBundle = EndpointsUtils.toBundle(request);
				requestBundle.putLong("requestId", request.getId());
			} else if(savedInstanceState!=null)
				requestBundle = savedInstanceState.getBundle("request");
				
			builder
				.setView(view)
				.setTitle(title)
				.setPositiveButton(R.string.request_button_text,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							ResponseFragmentListener listener = (ResponseFragmentListener) getActivity();
							if(listener!=null) {
								
								Response response = new Response();
								response.setRequestId(requestBundle.getLong("requestId"));
								
								TextView msgView = (TextView) view.findViewById(R.id.dialog_accept_message);
								response.setMessage(String.valueOf(msgView.getText()));
								
								listener.send(response);
							}
						}
					});
			
		} catch(Exception e) {
			Log.e(SolidalApp.TAG, "Error creating dialog in "+getClass(), e);
		}
	
		// Create the AlertDialog object and return it
		return builder.create();
	}

	@Override
	public void onSaveInstanceState(Bundle data) {
		data.putBundle("request", requestBundle);
		super.onSaveInstanceState(data);
	}

}
